package com.zealcomm.ivcscustomer.login

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.MotionEvent
import android.view.View
import com.zco.util.StringUtil
import com.zco.util.widget.MyProgressDialog
import com.zco.base.ServerCallback
import com.zco.base.entity.TokenBody
import com.zealcomm.commonui.base.BaseFragmentActivity
import com.zealcomm.ivcscustomer.BuildConfig
import com.zealcomm.ivcscustomer.R
import com.zealcomm.ivcscustomer.app.IvcsApplication
import com.zealcomm.ivcscustomer.app.IvcsApplication.Companion.loginSession
import com.zealcomm.ivcscustomer.databinding.ActivityLoginBinding
import com.zealcomm.ivcscustomer.service.MainActivity

class LoginActivity : BaseFragmentActivity() {

    private lateinit var binding: ActivityLoginBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setListeners()
        if (BuildConfig.DEBUG) {
            binding.etUserName.setText("123456")
            binding.etPassword.setText("123456")
            binding.etAgency.setText("123456")

//            binding.etUserName.setText("fk001")
//            binding.etPassword.setText("111111")
//            binding.etAgency.setText("t5")
        }
    }

    private fun setListeners() {
        var isHidePwd = true // 输入框密码是否是隐藏的，默认为true
        val drawables = binding.etPassword.compoundDrawables
        val eyeWidth = drawables[2].bounds.width() // 眼睛图标的宽度
        val drawableEyeOpen = resources.getDrawable(R.mipmap.icon_eye_open, theme)
        val drawableEyeClose = resources.getDrawable(R.mipmap.icon_eye_close, theme)
        drawableEyeOpen.bounds = drawables[2].bounds //这一步不能省略
        drawableEyeClose.bounds = drawables[2].bounds
        binding.etPassword.setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_UP) {
                // getWidth,getHeight必须在这里处理
                val et_pwdMinX = v.width - eyeWidth - binding.etPassword.paddingRight
                val et_pwdMaxX = v.width
                val et_pwdMinY = 0f
                val et_pwdMaxY = v.height
                val x = event.x
                val y = event.y
                if (x < et_pwdMaxX && x > et_pwdMinX && y > et_pwdMinY && y < et_pwdMaxY) {
                    // 点击了眼睛图标的位置
                    isHidePwd = !isHidePwd;
                    if (isHidePwd) {
                        binding.etPassword.setCompoundDrawables(
                            drawables[0], drawables[1], drawableEyeClose, drawables[3]
                        )
                        binding.etPassword.transformationMethod =
                            PasswordTransformationMethod.getInstance();
                    } else {
                        binding.etPassword.setCompoundDrawables(
                            drawables[0], drawables[1], drawableEyeOpen, drawables[3]
                        )
                        binding.etPassword.transformationMethod =
                            HideReturnsTransformationMethod.getInstance();
                    }
                }
            }
            false;
        }
        binding.etUserName.addTextChangedListener(textChangeListener)
        binding.etPassword.addTextChangedListener(textChangeListener)
        binding.etAgency.addTextChangedListener(textChangeListener)
    }

    fun onClick(view: View) {
        when (view.id) {
            R.id.btn_login -> {
                MyProgressDialog.show(this)
                binding.btnLogin.isEnabled = false
                loginSession(
                    binding.etUserName.text.toString(),
                    binding.etPassword.text.toString(),
                    binding.etAgency.text.toString(), object : ServerCallback<TokenBody>() {

                        override fun onSuccess(t: TokenBody?) {
                            runOnUiThread {
                                if (null == t) {
                                    binding.btnLogin.isEnabled = true
                                    MyProgressDialog.dismissProgress()
                                } else {
                                    IvcsApplication.customerTokenBody = t
                                    val bundle = Bundle()
                                    bundle.putString("picture", t.data.profile.picture)
                                    bundle.putString("fullName", t.data.profile.fullName)
                                    bundle.putString("gender", t.data.profile.gender)
                                    startActivity(MainActivity::class.java, bundle)
                                    binding.btnLogin.isEnabled = true
                                    MyProgressDialog.dismissProgress()
                                }
                            }
                        }

                        override fun failedResponse(result: String?) {
                            showToast(result)
                            runOnUiThread {
                                binding.btnLogin.isEnabled = true
                                MyProgressDialog.dismissProgress()
                            }
                        }
                    })
            }
            R.id.btn_setting -> startActivity(SettingActivity::class.java)
            else -> {
            }
        }
    }


    private val textChangeListener = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
        override fun afterTextChanged(s: Editable) {
            binding.btnLogin.isEnabled = !StringUtil.hasEmptyValue(
                binding.etUserName.text.toString(),
                binding.etPassword.text.toString(),
                binding.etAgency.text.toString()
            )
        }
    }
}